<?php
$msg = '&nbsp;';
if (isset($_REQUEST['applica'])) {
	$msg = applica();
}
if (!isset($_REQUEST['dir'])) $_REQUEST['dir'] = '/var/local/pre-vip/fs/file';
?>
<html>
<HEAD>
<META Name="expires" CONTENT= "Mon, 1 Jan 2000 00:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="content-language" content="it">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=windows-1251">
<style>
</style>
<title>u2gChmod</title>
</HEAD>
   <script LANGUAGE="javascript">
   </script>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method='POST' name='objFrm'>
<table border='1'>
   <tr>
      <td>         
         Applica diritti user a gruppo
      </td>
   </tr>
   <tr>
      <td >
         Directory : 
      	<input type='text' name='dir' value="<?=stripslashes(htmlentities($_REQUEST['dir']))?>" size='100'>
      </td>
   </tr>
   <tr>
      <td align='center'>
      	<input type='submit' name='applica' value="applica">
      </td>
   </tr>
   <tr>
      <td style='font-family: courier;'>
      	<?=$msg;?>
      </td>
   </tr>
</table>
</form>
</body>
</html>
<?
function applica() {
	$msg = '';
	$dir = stripslashes($_REQUEST['dir']);
	if (substr($dir,-1)=='/') $dir = substr($dir,0,-1);
	if (!is_dir($dir)) {
		return "directory non valida !";
	}
	zScanDir($dir, $msg);
	return $msg;
}

function zScanDir($dir, &$msg) {
	$dh  = opendir($dir);
	while (false !== ($fileName = readdir($dh))) {
		if($fileName[0]=='.') continue;
		$filePath = $dir."/".$fileName;
		$sPerm = zPerm($filePath);
		
		$sWR = (is_writable($filePath))?'W':'R';
		if ($sWR == 'W') {
				$mod = (@chmod($filePath,0775))?'mod':'** err**';
		} else {
			$mod = '';
		}		
		$msg .= fileowner($filePath)." ".filegroup($filePath)." ".$sPerm." ".$filePath." ($sWR) $mod<br>\r\n";
		if (is_dir($filePath)) {
			zScanDir($filePath, $msg);
		} 
	}
	closedir($dh);
}

function zPerm($filePath) {
	$perms = fileperms($filePath);
	if (($perms & 0xC000) == 0xC000) {
	   // Socket
	   $info = 's';
	} elseif (($perms & 0xA000) == 0xA000) {
	   // Symbolic Link
	   $info = 'l';
	} elseif (($perms & 0x8000) == 0x8000) {
	   // Regular
	   $info = '-';
	} elseif (($perms & 0x6000) == 0x6000) {
	   // Block special
	   $info = 'b';
	} elseif (($perms & 0x4000) == 0x4000) {
	   // Directory
	   $info = 'd';
	} elseif (($perms & 0x2000) == 0x2000) {
	   // Character special
	   $info = 'c';
	} elseif (($perms & 0x1000) == 0x1000) {
	   // FIFO pipe
	   $info = 'p';
	} else {
	   // Unknown
	   $info = 'u';
	}
	
	// Owner
	$info .= (($perms & 0x0100) ? 'r' : '-');
	$info .= (($perms & 0x0080) ? 'w' : '-');
	$info .= (($perms & 0x0040) ?
	           (($perms & 0x0800) ? 's' : 'x' ) :
	           (($perms & 0x0800) ? 'S' : '-'));
	
	// Group
	$info .= (($perms & 0x0020) ? 'r' : '-');
	$info .= (($perms & 0x0010) ? 'w' : '-');
	$info .= (($perms & 0x0008) ?
	           (($perms & 0x0400) ? 's' : 'x' ) :
	           (($perms & 0x0400) ? 'S' : '-'));
	
	// World
	$info .= (($perms & 0x0004) ? 'r' : '-');
	$info .= (($perms & 0x0002) ? 'w' : '-');
	$info .= (($perms & 0x0001) ?
	           (($perms & 0x0200) ? 't' : 'x' ) :
	           (($perms & 0x0200) ? 'T' : '-'));
	           
	return $info;
}
?>
